﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BDElectron.Remote;
using BDElectron.Platform;
using BDElectron.ClientSession;
using BDElectron.Utils;

namespace BDElectron.Server
{
    class ClientSession : MarshalByRefObject, IClientSession
    {
        private string mLastError = string.Empty;
        private IServerObject mServerObject = null;
        DataBridge mDataBridge = null;

        public ClientSession()
        {
        }
        
        public override object InitializeLifetimeService()
        {
            return null;
        }

        string IClientSession.LastError
        {
            get { return mLastError; }
        }

        object IClientSession.GetServerObjectThoughAuthentication(string login, string password)
        {
            mServerObject = (IServerObject)new ServerObject(login, password);
            if (mServerObject != null && mServerObject.User.Equals(string.Empty))
            {
                mServerObject = null;
            }

            return mServerObject;
        }

        string IClientSession.GetMd5Hash(string password)
        {
            //password = "1";
            string result = string.Empty;
            Crypt cryptString = new Crypt();
            result = cryptString.GetMd5Hash(password);
            return result;
        }

        #region Client Update Related
        string[] IClientSession.GetClientFilesList()
        {
            string[] files = null;

            try
            {
                string applicationDirectory = AppDomain.CurrentDomain.BaseDirectory;

                string clientDirectory = applicationDirectory + "Modules" + Path.DirectorySeparatorChar + "Client";
                FileInfo[] filesInfo = new DirectoryInfo(clientDirectory).GetFiles("*");
                files = new string[filesInfo.Length];
                for (int i = 0; i < filesInfo.Length; i++)
                {
                    FileInfo fileInfo = filesInfo[i];
                    files[i] = fileInfo.Name;
                }
            }
            catch (Exception e)
            {
                mLastError = "Server failed to access client module files. ";

                Logger.LogError(mLastError + e.Message);
            }

            return files;
        }

        byte[] IClientSession.GetClientFile(string fileName)
        {
            byte[] fileBytes = null;

            try
            {

                string applicationDirectory = AppDomain.CurrentDomain.BaseDirectory;


                string filePath = Path.Combine(applicationDirectory, "Modules") + Path.DirectorySeparatorChar + "Client" + Path.DirectorySeparatorChar + fileName;
                fileBytes = File.ReadAllBytes(filePath);
            }
            catch (Exception e)
            {
                mLastError = "Server failed to access client module file \"" + fileName + "\". ";
                Logger.LogError(mLastError + e.Message);
            }

            return fileBytes;
        }
        #endregion Client Update Related


        bool IClientSession.CheckDbAccess()
        {
            bool result = false;

            mDataBridge = new DataBridge();

            try
            {
                if (mDataBridge.Connect() == true)
                {
                    string commandText = "SELECT COUNT(Login) AS UsersCount FROM Users ";
                    System.Data.DataTable queryResult = mDataBridge.ExecuteQuery(commandText);
                    mLastError = mDataBridge.LastError;

                    if (queryResult != null && mLastError.Equals(string.Empty))
                    {
                        result = true;
                    }
                }
                else
                {
                    throw new Exception(mDataBridge.LastError);
                }
            }
            catch (Exception e)
            {
                mLastError = e.Message;
            }
            finally
            {
                mDataBridge.Disconnect();
            }

            return result;
        }
    }
}
